<?php

namespace Modules\Admin\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Laracasts\Flash;
use Modules\Admin\Http\Requests\UserRequest;
use Modules\Mall\Entities\Mall;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$users=User::paginate(10);
        return view('admin::user.index',compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(UserRequest $request)
    {
    	User::create([
    		'name'=>$request->input('name'),
    		'email'=>$request->input('email'),
			'password'=>bcrypt($request->input('password')),
			'status'=>1
		]);
    	flash('添加成功！')->overlay ();
    	return redirect ()->back();
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(UserRequest $request,$id)
    {
    	$user=User::find($id);
    	$user->name=$request->input('name');
    	$user->email=$request->input('email');
    	if($request->input('password')){
    		$user->password=bcrypt($request->input('password'));
		}
		$user->save();
    	flash('修改成功！')->overlay ();
    	return redirect ()->back();
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
    	$m=Mall::where('owner',$id)->first();
    	if($m){
    		flash('该商户下有店铺，请先将名下店铺删除再来删除商户！')->overlay ();
    		return redirect ()->back ();
		}
    	User::destroy ($id);
    	flash('删除成功！')->overlay ();
    	return redirect ()->back();
    }
    public function showmall($id){
    	$data=Mall::where('owner',$id)->paginate(10);
    	return view('mall::mall.index',compact('data'));
	}
}
